<!DOCTYPE html>
<html>
<head>
<title>Electromagnetic Field Diagram</title>
</head>
<body>
<canvas id="myCanvas" width="500" height="450" style="border:1px solid #d3d3d3;"></canvas>
<script>
function drawArrow(ctx, fromx, fromy, tox, toy, headlen = 10) {
    const dx = tox - fromx;
    const dy = toy - fromy;
    const angle = Math.atan2(dy, dx);
    ctx.moveTo(fromx, fromy);
    ctx.lineTo(tox, toy);
    ctx.lineTo(tox - headlen * Math.cos(angle - Math.PI / 6), toy - headlen * Math.sin(angle - Math.PI / 6));
    ctx.moveTo(tox, toy);
    ctx.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6), toy - headlen * Math.sin(angle + Math.PI / 6));
}

const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');

const cx = 250;
const cy = 200;

ctx.strokeStyle = 'black';
ctx.fillStyle = 'black';
ctx.lineWidth = 2;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';


// Draw the large loop (ellipse)
ctx.beginPath();
ctx.ellipse(cx, cy, 100, 180, 0, 0, 2 * Math.PI);
ctx.stroke();

// Draw the small sphere (circle) at the center
const radius_a_sphere = 30;
ctx.beginPath();
ctx.arc(cx, cy, radius_a_sphere, 0, 2 * Math.PI);
ctx.stroke();

// Draw the cross symbol 'X' inside the small circle
ctx.beginPath();
const cross_radius = radius_a_sphere * 0.8;
const diag = cross_radius / Math.sqrt(2);
ctx.moveTo(cx - diag, cy - diag);
ctx.lineTo(cx + diag, cy + diag);
ctx.moveTo(cx + diag, cy - diag);
ctx.lineTo(cx - diag, cy + diag);
ctx.stroke();

// Draw the z-axis
ctx.beginPath();
drawArrow(ctx, cx - 130, cy, cx + 200, cy);
ctx.stroke();
ctx.font = "italic 20px 'Times New Roman'";
ctx.fillText("z", cx + 215, cy + 2);

// Draw the line for radius 'a'
ctx.beginPath();
ctx.moveTo(cx, cy);
ctx.lineTo(cx - radius_a_sphere, cy);
ctx.stroke();
ctx.fillText("a", cx - 45, cy - 12);


// Draw the line for radius R
ctx.beginPath();
// The original R line is diagonal to the lower left.
const angle_R = Math.PI * 1.35; 
const ellipse_rx = 100;
const ellipse_ry = 180;
const endRx = cx + ellipse_rx * Math.cos(angle_R);
const endRy = cy + ellipse_ry * Math.sin(angle_R);
ctx.moveTo(cx, cy);
ctx.lineTo(endRx, endRy);
ctx.stroke();
ctx.fillText("R", cx - 70, cy + 110);


// Draw the B0 vector
ctx.beginPath();
ctx.lineWidth = 4; // B0 vector is thicker
drawArrow(ctx, cx, cy, cx + 80, cy, 12);
ctx.stroke();
ctx.lineWidth = 2; // Reset line width
ctx.fillText("B", cx + 45, cy + 20);
ctx.font = "italic 16px 'Times New Roman'";
ctx.fillText("0", cx + 57, cy + 25);
ctx.font = "italic 20px 'Times New Roman'";


// Draw the n vector and r label
const theta = -Math.PI / 4.5; // Angle adjusted slightly to match image
const len_n = 110;
const nx_end = cx + len_n * Math.cos(theta);
const ny_end = cy + len_n * Math.sin(theta);
ctx.beginPath();
drawArrow(ctx, cx, cy, nx_end, ny_end);
ctx.stroke();
ctx.fillText("n", nx_end - 15, ny_end - 15);
ctx.fillText("r", nx_end + 15, ny_end + 5);

// Draw the angle theta
ctx.beginPath();
const arc_radius_theta = 50;
ctx.arc(cx, cy, arc_radius_theta, 0, theta, true);
ctx.stroke();
ctx.fillText("θ", cx + arc_radius_theta - 5, cy - 20);


// Draw the figure caption
ctx.font = "20px 'Times New Roman'";
ctx.fillText("Fig. 2.52", cx, cy + 220);

</script>
</body>
</html>